package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallShopAuditEntity;
import net.lahuobao.mall.entity.admin.MallShopEntity;
import net.lahuobao.mall.entity.admin.MallShopSettlePeriodEntity;
import net.lahuobao.mall.service.admin.MallShopAuditServiceI;
import net.lahuobao.mall.service.admin.MallShopSettlePeriodServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.lahuobao.mall.service.admin.MallShopServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

@Service("mallShopService")
@Transactional
public class MallShopServiceImpl extends CommonServiceImpl implements MallShopServiceI {

    @Autowired
    private MallShopAuditServiceI auditService;

    @Autowired
    private MallShopSettlePeriodServiceI mallShopSettlePeriodService;
    /**
     * 更新店铺审核状态
     * @param mallShopAudit
     */
    @Override
    public void upStatus(MallShopAuditEntity mallShopAudit) {

        //保存审核记录信息
        mallShopAudit.setCreatetime(new Date());
        auditService.save(mallShopAudit);

        //更新店铺状态
        String sql = " update lahuobao.mall_shop set shopStatus = ? where id = ? ";
        super.executeSql(sql, mallShopAudit.getAuditstatus(), mallShopAudit.getShopid());

        //如果第一次申请开店并通过插入店铺结算规则表
        if(mallShopAudit.getAuditstatus().toString().equals("1")){
            //查询店铺的结算规则
            List<MallShopSettlePeriodEntity>  lists =
                    mallShopSettlePeriodService.findByProperty(MallShopSettlePeriodEntity.class, "shopid", mallShopAudit.getShopid());
            if(lists.size() == 0){
                //获取默认结算相关信息
                ResourceBundle bundle = PropertyResourceBundle.getBundle("shopping");
                int settlDayCount = Integer.parseInt(bundle.getString("settl_day_count"));
                BigDecimal settleRate = new BigDecimal(bundle.getString("settle_rate"));
                BigDecimal settleRateAdvance = new BigDecimal(bundle.getString("settle_rate_advance"));
                //构建店铺结算信息
                MallShopSettlePeriodEntity shopSettlePeriod = new MallShopSettlePeriodEntity();
                shopSettlePeriod.setShopid(mallShopAudit.getShopid());
                shopSettlePeriod.setSettledaycount(settlDayCount);
                shopSettlePeriod.setSettlerate(settleRate);
                shopSettlePeriod.setSettlerateadvance(settleRateAdvance);
                shopSettlePeriod.setStatus(1);
                shopSettlePeriod.setCreatetime(new Date());
                shopSettlePeriod.setCreateuserid(mallShopAudit.getUserid());
                mallShopSettlePeriodService.save(shopSettlePeriod);
                getSession().flush();
            }
        }
    }
}